package com.start.manager.service.impl;

import com.start.manager.domain.vo.UserVO;
import com.start.manager.domain.entity.User;
import com.start.manager.dao.UserDao;
import com.start.manager.exception.UserException;
import com.start.manager.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.manager.util.MyUtil;
import com.start.manager.util.RedisUtil;
import com.start.manager.util.constant.MessageConstant;
import com.start.manager.util.constant.RedisKeyConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lijian
 * @since 2019-12-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public String login(UserVO userVO) {
        User user = baseMapper.selectById(userVO.getId());
        if (Objects.isNull(user)){
            throw new UserException(MessageConstant.NO_USER);
        }

        if (!StringUtils.equals(user.getPassword(), MyUtil.encoderMD5(userVO.getPassword()))){
            throw new UserException(MessageConstant.ERROR_PASSWORD);
        }
        String token = MyUtil.generateToken(userVO.getId());
        //存入redis中
        redisUtil.hset(RedisKeyConstant.USER_TOKEN,token,"true");
        return token;
    }
}
